/**
 * 电话会议控制层
 */
define([ "app", "jquery", "md5", "ecb", 
        "services/UserService",
		"resources/UserResource", 
		'directives/InfiniteScroll',
		'directives/ColumnList',
		'directives/conferenceAuthority/conferenceAuthoritys',
		"directives/addCompany/AddCompany",
		'directives/addCompany/Company',
		"resources/ConferenceAuthorityResource" ], function(app) {

	var deps = [ "$scope", "$state", "$modal","$location", "$stateParams","UserService","UserResource","ConferenceAuthorityResource" ];

	 
	function controller($scope, $state, $modal,$location, $stateParams,UserService,UserResource,ConferenceAuthorityResource) {
		var pageNo = 1;
		var pageSize = 99999;
		queryDate();
		
		//var currentUser =UserService.getCurrentUser();
		$scope.changeLogoutImage =function (flag){
			var image =document.getElementById('imageChanges');
			if(flag){
				image.src="images/logout2.png";
			}else{
				image.src="images/logout1.png";
			}
		};
		
		$scope.changeChargeImage=function (flag){
			var image =document.getElementById('changeChargeImage');
			if(flag){
				image.src="images/charge2.png";
			}else{
				image.src="images/charge1.png";
			}
		};
		
		$scope.$on('companyFBroadCast', function($event,data) {
        	$scope.conferenceAuthoritys=null;
			refreshDate(0,pageSize);
			$event.preventDefault();
        });
		
		$scope.addCompany=function addCompany($choseCompany){
			$scope.conferenceAuthoritys=null;
			queryDate();
		};
		
		
		$scope.changeAddImage=function (flag){
			var image =document.getElementById('changeAddImage');
			if(flag){
				image.src="images/add2.png";
			}else{
				image.src="images/add1.png";
			}
		};
		
		/**
		 * 退出登陆
		 */
		$scope.logout = function() {
			UserResource.logout(function(){
				UserService.removeCurrentUser();
				$location.path("/login");
			});
		};
		
		$scope.scrollHandler = function scrollHandler() {
			pageNo++;
			queryDate().success(function loadedScrollCompanys(conferenceAuthoritys) {
				if (conferenceAuthoritys == null || conferenceAuthoritys.length < pageSize) {
					$scope.scrollHandler = function noop() {};
				}
			});
		};
		
		$scope.$bus.subscribe({
			conferenceAuthority: 'conferenceAuthority',
			topic: 'remove',
			callback: function(id) {
				_.remove($scope.conferenceAuthoritys, function(conferenceAuthority) {
					return conferenceAuthority.id === id;
				});
			}
		});
		
		// 初始化加载数据
		function queryDate(){
			return ConferenceAuthorityResource.queryConferenceAuthorityList(pageNo,pageSize,null).success(function loadedCompanys(conferenceAuthoritys) {
				$scope.conferenceAuthoritys = $scope.conferenceAuthoritys || [];
				[].push.apply($scope.conferenceAuthoritys, conferenceAuthoritys);
			});
		}
		
		// 刷新数据
		function refreshDate(pageNo,pageSize){
			return ConferenceAuthorityResource.queryConferenceAuthorityList(pageNo,pageSize,null).success(function loadedCompanys(conferenceAuthoritys) {
				$scope.conferenceAuthoritys = $scope.conferenceAuthoritys || [];
				[].push.apply($scope.conferenceAuthoritys, conferenceAuthoritys);
			});
		}
	}
	controller.$inject = deps;
    return app.lazy.controller("ConferenceManagementController", controller);
});
